package com.example.sharpshopdemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sharpshopdemo.entity.Dict;
import com.example.sharpshopdemo.entity.Result;
import com.example.sharpshopdemo.entity.Role;
import com.example.sharpshopdemo.mapper.DictMapper;
import com.example.sharpshopdemo.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl
        extends ServiceImpl<DictMapper, Dict>
        implements DictService {
    @Autowired
    private Result result;

    @Autowired
    private DictMapper dictMapper;


    @Override
    public Result getAll() {
        result.setSuccess(true);
        result.setData(this.list());
        return result;
    }

    // 插入数据
    public Result insertData(Dict dict) {
        Result result = new Result(); // 假设Result是一个用于封装响应结果的类
        boolean success = dictMapper.insert(dict) > 0;
        result.setSuccess(success);
        return result;
    }

    // 更新数据
    public Result updateData(Dict dict) {
        Result result = new Result();
        boolean success = dictMapper.updateById(dict) > 0; // 假设updateById是根据DataDict中的某个唯一标识更新数据
        result.setSuccess(success);
        return result;
    }

    // 删除数据
    public Result deleteData(Long id) { // 假设我们根据ID来删除数据
        Result result = new Result();
        boolean success = dictMapper.deleteById(id) > 0; // 假设deleteById接受一个Long类型的ID作为参数
        result.setSuccess(success);
        return result;
    }

    // 查询数据（这里只是一个示例，具体实现取决于查询需求）

    public Result<List<Dict>> findDictsByNameLike(String keyword) {
        List<Dict> dicts = dictMapper.selectDictsByNameLike(keyword);
        Result<List<Dict>> result = new Result<>();
        if (dicts != null && !dicts.isEmpty()) {
            result.setSuccess(true);
            result.setData(dicts);
        } else {
            result.setSuccess(false);
            result.setErrorMsg("未找到匹配的角色");
        }
        return result;
    }
}
